//
//  CollectViewController.m
//  自定义UITabBarViewControll
//
//  Created by 尧的mac on 15/11/13.
//  Copyright © 2015年 尧的mac. All rights reserved.
//

#import "CollectViewController.h"
#import "XYMainController.h"
#import "CantactModel.h"
#import "XYInfoViewController.h"
#define PATH NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES)[0]
#define PATHS [PATH stringByAppendingPathComponent:@"x.plist"]

@interface CollectViewController ()<XYMainControllerdetegate>
@property(nonatomic,strong)NSMutableArray * array;

@property(nonatomic,weak)UITableViewCell *tableCell;
@end //

@implementation CollectViewController
-(NSMutableArray *)array{
    
    if (_array == nil) {
        //解档
        _array =  [NSKeyedUnarchiver unarchiveObjectWithFile:PATHS];
        
        if (_array ==nil) {
            _array = [NSMutableArray array];
            
        }
    }
    return _array;
}
- (void)viewDidLoad {
    [super viewDidLoad];

   self.title = @"收藏人信息";
    
    self.tableView.showsVerticalScrollIndicator = NO;
}

#pragma mark - UITableView的协议方法
//行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.array.count;
}

//单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString * ID = @"IDs";
    UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    CantactModel * model  = self.array[indexPath.row];
    
    cell.textLabel.text = model.name;
    cell.detailTextLabel.text = model.deText;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    self.tableCell = cell;
    return cell;
}

#pragma mark - XYImageTableViewController的协议方法
-(void)setCellData:(XYMainController *)tableView andName:(NSString *)Name andPhone:(NSString *)deText{
    
    CantactModel * model  = [[CantactModel alloc]init];
    model.name = Name;
    model.deText = deText;
    //将模型数据添加到数组
    [self.array addObject:model];
    
//    //刷新单元格
//    [self.tableView reloadData];
    //归档
    [NSKeyedArchiver archiveRootObject:self.array toFile:PATHS];
    
}

#pragma mark - 删除协议

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return YES;
}
#pragma mark ---选中哪行单元格
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //获取选中行单元格
    self.tableCell = [self.tableView cellForRowAtIndexPath:indexPath];
    XYInfoViewController * info = [[XYInfoViewController alloc]init];
    [info getInfo:self.tableCell.detailTextLabel.text];
    [self.navigationController pushViewController:info animated:YES];
    
    
  
}
//手势删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.array removeObject:self.array[indexPath.row]];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
    
    [self.tableView reloadData];
    //归档
    [NSKeyedArchiver archiveRootObject:self.array toFile:PATHS];
    
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return @"确定删除";
}

@end
